<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/5 15:51
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Wx extends ADM_Controller
{
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $this->load->view('admin/system/wx.html');
    }

    /**
     * 获取比例
     */
    public function getScale()
    {
        $this->load->model('admin/system/Wx_Model', 'setting');
        $res = $this->setting->getSetting('scale');

        $data = [
            'level_1' => value($res, 'level_1', ''),
            'level_2' => value($res, 'level_2', '')
        ];

        $this->output->send([
            'state' => true,
            'data' => $data
        ]);
    }

    /**
     * 设置修改比例
     */
    public function setScale()
    {
        $level_1 = $this->input->post('level_1');
        $level_2 = $this->input->post('level_2');

        $data = [
            'level_1' => !empty($level_1) ? $level_1 : 0,
            'level_2' => !empty($level_2) ? $level_2 : 0
        ];

        $this->load->model('admin/system/Wx_Model', 'setting');
        if ($this->setting->saveSetting($data, 'scale')) {
            $this->output->send([
                'state' => true,
                'msg' => '保存成功'
            ]);
        } else {
            $this->output->send([
                'state' => false,
                'msg' => '保存失败'
            ]);
        }
    }

    /**
     * 上传
     */
    public function upload()
    {
        $send['state'] = false;
        do {
            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $res = local($_FILES['file']);
            if (empty($res)) {
                $send['error'] = '上传失败';
            } else {
                $send['state'] = true;
                $send['data'] = array_merge($res, array('fullPath' => thumb($res['key'])));
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取海报
     */
    public function getBill()
    {
        $this->load->model('admin/system/Wx_Model', 'setting');
        $res = $this->setting->getSetting('bill');
        $this->output->send([
            'state' => true,
            'data' => empty($res) ? [] : $res
        ]);
    }

    /**
     * 保存海报设置
     */
    public function saveBill()
    {
        $send['state'] = false;
        do {

            $setting = $this->input->post();
            if (empty($setting)) {
                $send['error'] = '参数错误';
                break;
            }

            if (empty($setting['image'])) {
                $send['error'] = '请上传海报背景图片';
                break;
            }

            if (empty($setting['local']['x']) || empty($setting['local']['y'])) {
                $send['error'] = '请填写二维码起始坐标';
                break;
            }

            if (empty($setting['size']['width']) || empty($setting['size']['height'])) {
                $send['error'] = '请填写二维码尺寸大小';
                break;
            }

            $this->load->model('admin/system/Wx_Model', 'setting');
            if ($this->setting->saveSetting($setting, 'bill')) {
                // 更新所有已有用户的海报
                $this->load->model('admin/user/User_Model', 'user');
                $this->user->cleanBill();
                $send['state'] = true;
            } else {
                $send['error'] = '保存失败';
            }
        } while(false);
        $this->output->send($send);
    }
}
